package com.wonders.hisdoctor.station.project.hisqueue.controller;

import com.wonders.his.common.bean.BaseResult;
import com.wonders.his.common.utils.StringUtils;
import com.wonders.his.common.utils.validator.ValidateUtils;
import com.wonders.his.common.utils.validator.group.PrimaryGroup;
import com.wonders.hisdoctor.station.project.base.domain.HospitalDict;
import com.wonders.hisdoctor.station.project.hisqueue.domain.DiagnosisExplain;
import com.wonders.hisdoctor.station.project.hisqueue.service.IDiagnosisExplainService;
import com.wonders.hisdoctor.station.project.hisqueue.vo.DiagnosisExplainVo;
import com.wonders.hisdoctor.station.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhanghaonan
 * @date 2021/6/21
 * @Description
 */
@Api(tags = "诊断说明书")
@RestController
@RequestMapping("/diagnosisexplain")
public class DiagnosisExplainController {

    @Autowired
    private IDiagnosisExplainService diagnosisExplainService;

    @Autowired
    private CommonUtil commonUtil;

    @ApiOperation("保存并打印")
    @PostMapping("/saveAndPrint")
    public BaseResult<DiagnosisExplainVo> saveAndPrint(@RequestBody DiagnosisExplainVo diagnosisExplain) {
        DiagnosisExplain result = diagnosisExplainService.saveAndPrint(diagnosisExplain);
        DiagnosisExplainVo diagnosisExplainVo = new DiagnosisExplainVo();
        BeanUtils.copyProperties(result, diagnosisExplainVo);
        if (StringUtils.isNotEmpty(diagnosisExplain.getHospitalCode())) {
            HospitalDict hospitalDict = diagnosisExplainService.selectHospitalByCode(diagnosisExplain.getHospitalCode());
            if (hospitalDict != null) {
                diagnosisExplainVo.setTitle(commonUtil.setPrintTitle(diagnosisExplain.getHospitalCode()) + hospitalDict.getHospitalName());
            }
        }
        return BaseResult.success(diagnosisExplainVo);
    }

    @ApiOperation("主键查询")
    @PostMapping("/getById")
    public BaseResult<DiagnosisExplainVo> getById(@RequestBody DiagnosisExplainVo diagnosisExplain) {
        ValidateUtils.validateEntity(diagnosisExplain, new Class[]{PrimaryGroup.class});
        DiagnosisExplain result = diagnosisExplainService.getById(diagnosisExplain);
        if (result == null) {
            result = new DiagnosisExplain();
        }
        DiagnosisExplainVo diagnosisExplainVo = new DiagnosisExplainVo();
        BeanUtils.copyProperties(result, diagnosisExplainVo);
        if (StringUtils.isNotEmpty(diagnosisExplain.getHospitalCode())) {
            HospitalDict hospitalDict = diagnosisExplainService.selectHospitalByCode(diagnosisExplain.getHospitalCode());
            if (hospitalDict != null) {
                diagnosisExplainVo.setTitle(commonUtil.setPrintTitle(diagnosisExplain.getHospitalCode()) + hospitalDict.getHospitalName());
            }
        }
        return BaseResult.success(diagnosisExplainVo);
    }
}
